﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BlazeNotes.Core.Commands.UI;

namespace BlazeNotes.UI.Controls
{
    /// <summary>
    /// Interaction logic for RichTextBoxExt.xaml
    /// </summary>
    public partial class RichTextBoxExt
    {
        public RichTextBoxExt()
        {
            InitializeComponent();
        }

        private void OnInsertionChekcBoxExecute(object sender, ExecutedRoutedEventArgs e)
        {
            foreach (var currentBlock in this.CaretPosition.Paragraph.Inlines)
            {
                if (currentBlock is InlineUIContainer)
                {
                    var container = currentBlock as InlineUIContainer;
                    if (container.Child is CheckBox)
                    {
                        CaretPosition.Paragraph.Inlines.Remove(currentBlock);
                        return;
                    }
                }
            }

            var insertionPosition = this.CaretPosition.Paragraph.ElementStart;
            var checkBox = new CheckBox();

            checkBox.Unchecked += OnTextBoxCheckedChanged;
            checkBox.Checked += OnTextBoxCheckedChanged;

            new InlineUIContainer(checkBox, insertionPosition){ BaselineAlignment = BaselineAlignment.Center};

        }

        private void OnTextBoxCheckedChanged(object sender, RoutedEventArgs e)
        {
            var paragraph = ((sender as CheckBox).Parent as InlineUIContainer).Parent as Paragraph;
            TextRange range = new TextRange(paragraph.ContentStart, paragraph.ContentEnd);

            TextDecorationCollection tdc =
                (TextDecorationCollection)this.Selection.GetPropertyValue(Inline.TextDecorationsProperty);
            if (tdc == null || !tdc.Equals(TextDecorations.Strikethrough))
            {
                tdc = TextDecorations.Strikethrough;
            }
            else
            {
                tdc = new TextDecorationCollection();
            }
            range.ApplyPropertyValue(Inline.TextDecorationsProperty, tdc);
        }
    }
}
